package ru.jetsoft.reg112.desktop.data;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class ParamData {

    private final Param param;
    private List<String> values;
    private final List<ParamData> childs;

    public ParamData(Param param) throws IllegalArgumentException {
        if(param == null) {
            throw new IllegalArgumentException("param must nit be null");
        }
        this.param = param;
        values = new LinkedList<>();
        if(!param.isMultiple()) {
            values.add(null);
        }
        childs = new LinkedList<>();
    }

    public Param getParam() {
        return param;
    }

    public String getValue() throws IllegalArgumentException {
        if(param.isMultiple()) {
            throw new IllegalArgumentException("Operation is forbidden for multiple params");
        }
        return values.get(0);
    }

    public String getValue(int i) throws IllegalArgumentException {
        if(i < 0 || i >= values.size()) {
            throw new IllegalArgumentException("Operation is forbidden for multiple params");
        }
        return values.get(i);
    }

    public List<String> getValues() throws IllegalArgumentException {
        if(!param.isMultiple()) {
            throw new IllegalArgumentException("Operation is forbidden for multiple params");
        }
        return values;
    }

    public void setValue(String value) {
        if(param.isMultiple()) {
            throw new IllegalArgumentException("Operation is forbidden for multiple params");
        }
        values.clear();
        values.add(value);
    }

    public void addValue(String value) {
        if(!param.isMultiple()) {
            throw new IllegalArgumentException("Operation is forbidden for multiple params");
        }
        values.add(value);
    }

    public List<ParamData> getChilds() {
        return childs;
    }

    public void setChilds(Collection<ParamData> childs) {
        this.childs.clear();
        this.childs.addAll(childs);
    }

    public void addChilds(Collection<ParamData> childs) {
        this.childs.addAll(childs);
    }

    public void addChild(ParamData child) {
        childs.add(child);
    }
}
